#!/bin/bash

usage="Usage: ./bench.sh threads osd_pool/rbd_device keyring_file client_name"

if [ $# -ne 4 ]; then
	echo $usage
else
blocksize=("4K" "64K" "256K" "2M" "4M")
#blocksize=("4K")
RECFILE="./bench.log"

touch $RECFILE
> $RECFILE  ##clear all records in log file.

for (( i = 0; i < ${#blocksize[@]}; i++ ))
do
	echo "Sequence writing blocksize: ${blocksize[$i]}Byte to $2 ... ..."
	echo "> seq write ${blocksize[$i]}B" >> $RECFILE  ## '>' flag-char
	#dd if=/dev/zero of=$1 count=$cnt bs=${blocksize[$i]} >> $RECFILE 2>&1
	rbd bench --io-type write  $2 --io-pattern seq --io-threads $1 --io-size ${blocksize[$i]} --keyring=$3 --user $4 >> $RECFILE 2>&1
	echo "Random writing blocksize: ${blocksize[$i]}Byte to $2 ... ..."
	echo "> rand write ${blocksize[$i]}B" >> $RECFILE
	rbd bench --io-type write  $2 --io-pattern rand --io-threads $1 --io-size ${blocksize[$i]} --keyring=$3 --user $4 >> $RECFILE 2>&1
	echo "Sequence reading blocksize: ${blocksize[$i]}Byte to $2 ... ..."
	echo "> seq read ${blocksize[$i]}B" >> $RECFILE
	rbd bench --io-type read  $2 --io-pattern seq --io-threads $1 --io-size ${blocksize[$i]} --keyring=$3 --user $4 >> $RECFILE 2>&1
	echo "Random reading blocksize: ${blocksize[$i]}Byte to $2 ... ..."
	echo "> rand read ${blocksize[$i]}B" >> $RECFILE
	rbd bench --io-type read  $2 --io-pattern rand --io-threads $1 --io-size ${blocksize[$i]} --keyring=$3 --user $4 >> $RECFILE 2>&1
done

cat $RECFILE | while read LINE  
do
	if [[ ${LINE:0:1} == ">" ]] ; then 
		echo -e "$LINE\t\t\c"
	fi
	#echo $LINE | awk '$1=="elapsed:" {print $0}'
	echo $LINE | awk '$1=="elapsed:" {print $5" "$6 "\t" $7" "$8}'
done
fi